// pages/activity/index.js
//获取应用实例
var app = getApp();
var md5 = require("../../utils/md5.js");
var payUserInfo = wx.getStorageSync('payUserInfo');
var payUserPhone = wx.getStorageSync('payUserPhone');
// var payUserInfo = '';
// var payUserPhone = '';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,//用户信息获取弹窗
    showModalStatusPhone:false,//用户手机号获取弹窗
    phone:0,
    qiang_money_display:'',
    shop_id:0,
    shop_name:'',
    shop_pic_small:'',
    dikou_money_type:true,
    qiang_money:0,
    openid:0,
    imageUrl:4,
    xcx_openid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.showLoading({
      title: '加载数据',
    });
    var that = this;
    var scene = options.scene;
    if (scene != "undefined" && scene != "") {
      
      that.setData({
        shop_id: scene,
      });
      wx.setStorageSync('shop_id', scene);

      wx.login({
        success: function (res_code) {
          console.log(res_code);
          wx.request({

            url: app.globalData.erpUrl + '/web/get_wx_open_id',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              code: res_code.code,
            },
            success: function (res) {
              that.setData({
                xcx_openid: res.data.openid,
              })
              that.CheckXcxOpenid();
            }
          });
        }
      })
    } else {
      wx.showLoading({
        title: '店铺不正确',
        icon: 'none'
      })
    }
  },

  /**
   * 验证并且取出用户信息
   */
  CheckXcxOpenid: function(){
    //开始验证注册用户
    var that = this; 
    var xcx_openid = that.data.xcx_openid;
    var shop_id = that.data.shop_id;
    var sign = md5.md5('User.CheckXcxOpenid' + shop_id + xcx_openid);
    wx.request({
      url: app.globalData.serviceUrl + 'User.CheckXcxOpenid',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        sign: sign,
        xcx_openid: xcx_openid,
        shop_id: shop_id
      },
      success: function (res) {
        var result = res.data.data;
        if (result.code == 0) {
          that.setData({
            openid: result.info.openid
          });
          that.QiangShopRedSaoMa();
        }else{
          wx.hideLoading();
          that.util('open', 'user_pone');
        }
      }
    })
  },

  /**
  * 验证手机号并且取出用户信息
  */
  CheckXcxOpenidAndTel: function () {
    //开始验证注册用户
    var that = this;
    var reg_tel = that.data.phone;
    var xcx_openid = that.data.xcx_openid;
    var shop_id = that.data.shop_id;
    var sign = md5.md5(reg_tel + 'User.CheckXcxOpenidAndTel' + shop_id + xcx_openid);
    wx.request({
      url: app.globalData.serviceUrl + 'User.CheckXcxOpenidAndTel',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        sign: sign,
        xcx_openid: xcx_openid,
        shop_id: shop_id,
        reg_tel: reg_tel,
      },
      success: function (res) {
        var result = res.data.data;
        if (result.code == 0) {
          that.setData({
            openid: result.info.openid
          });
          that.util('close', 'user_pone');
          that.QiangShopRedSaoMa();
        } else {
          that.util('close', 'user_pone');
          that.util('open', 'user_info');
        }
      }
    })
  },

  /**
  * 用户不存在注册新用户
  */
  CheckUserAll: function (e) {
    //开始验证注册用户
    var that = this;
    var avatar = e.detail.userInfo.avatarUrl;
    var nickName = that.replace(e.detail.userInfo.nickName);
    var sex = e.detail.userInfo.gender;
    var reg_tel = that.data.phone;
    var xcx_openid = that.data.xcx_openid;
    var shop_id = that.data.shop_id;
    var sign = md5.md5(avatar + nickName + reg_tel + 'User.CheckUserAll' + sex + shop_id + xcx_openid);
    wx.request({
      url: app.globalData.serviceUrl + 'User.CheckUserAll',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        sign: sign,
        xcx_openid: xcx_openid,
        shop_id: shop_id,
        reg_tel: reg_tel,
        sex: sex,
        nickName: nickName,
        avatar: avatar,
      },
      success: function (res) {
        var result = res.data.data;
        if (result.code == 0) {
          that.setData({
            openid: result.info.openid
          });
          that.util('close', 'user_info');
          that.QiangShopRedSaoMa();
        } else {
          that.util('open', 'user_info');
        }
      }
    })
  },




  //过滤emoji表情符号
  replace: function (nickName){
    return nickName.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
  },

  /**
   * 获取用户扫码获得金额
   */
  QiangShopRedSaoMa:function(){
    var that = this;
    var service = 'Red.QiangShopRedSaoMa';
    var openid = that.data.openid;
    var shop_id = that.data.shop_id;
    var sign = md5.md5(openid + service + shop_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        openid: openid,
        shop_id: shop_id,
      },
      success: (res) => {
        var result = res.data.data;
        console.log(result);
        console.log('扫码取出店铺数据');
        if (result.code == 0) {
          that.setData({
            qiang_money: result.info.qiang_money,
            imageUrl: 1,
            qiang_money_display:'待领取',
          })
          if (result.info.shop_data.is_dikou > 0){
            that.setData({
              dikou_money_type:false,
              dikou_money: result.info.shop_data.dikou_money,
            })
          }
        } else {
          wx.showToast({
            title: result.info.message,
            icon: 'success',
            duration: 2000
          })
          if (result.info.status == 2){
            that.setData({
              imageUrl: 2,
              qiang_money_display: '已领取'
            })
            that.getStart();
          }else{
            that.setData({
              imageUrl: 3,
              qiang_money_display: '已抢完'
            })
             that.getStart();
          }
        }
        that.setData({
          shop_name: result.info.shop_data.shop_name,
          shop_pic_small: result.info.shop_data.shop_pic_small,
        })
        wx.hideLoading();
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //显示数据
  display:function(){
    this.setData({
      imageUrl:2,
      qiang_money_display: '¥'+ this.data.qiang_money,
    })
    this.getStart();
  },

  //三秒后执行跳转
  getStart:function(){
    var that = this;
    setTimeout(function () {
      wx.reLaunch({
        url: "/pages/jingxuan/index?shop_id=" + that.data.shop_id
      })
    }, 3000);
  },


  //获取用户手机号
  getPhoneNumber: function (e) {
    var that = this;
    wx.login({
      success: function (res) {
        //以下开始获取手机号
        var phone = "";
        if (e.detail.errMsg == 'getPhoneNumber:ok') {
          wx.request({
            url: 'https://mylife.zntech-china.com/web/get_wx_phone',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              'encryptedData': e.detail.encryptedData,
              'iv': e.detail.iv,
              'code': res.code
            },
            success: function (res) {
              phone = res.data.phone.phoneNumber
              that.setData({
                phone: phone,
                user_pone: false
              });
              that.CheckXcxOpenidAndTel();
            }
          })
        }else{
          wx.showLoading({
            title: '获取手机号失败',
            icon: 'none'
          })
        }
      }
    })
  },

  //弹窗动画
  util: function (currentStatu,type_val) {
    console.log(currentStatu);
    console.log(type_val);
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        if (type_val == "user_info"){
          this.setData(
            {
              showModalStatus: false
            }
          );
        } else if (type_val == "user_pone"){
          this.setData(
            {
              showModalStatusPhone: false
            }
          );
        }
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      if (type_val == "user_info") {
        this.setData(
          {
            showModalStatus: true
          }
        );
      } else if (type_val == "user_pone") {
        this.setData(
          {
            showModalStatusPhone: true
          }
        );
      }
    }
  },
})